<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('insurance_payments', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 关联信息
            $table->unsignedBigInteger('insurance_record_id');
            $table->unsignedBigInteger('employee_id');
            $table->string('employee_name', 100)->nullable();
            $table->string('employee_number', 50)->nullable();

            // 缴费信息
            $table->string('payment_number', 50)->unique(); // 缴费编号
            $table->string('payment_period', 20); // 2024-01, 2024-Q1, 2024-H1, 2024
            $table->date('payment_date'); // 缴费日期
            $table->date('due_date'); // 应缴日期

            // 缴费金额
            $table->decimal('base_amount', 10, 2); // 缴费基数
            $table->decimal('employee_contribution', 10, 2)->default(0); // 个人缴费
            $table->decimal('company_contribution', 10, 2)->default(0); // 单位缴费
            $table->decimal('total_amount', 10, 2); // 总缴费金额

            // 缴费状态
            $table->string('payment_status', 20)->default('pending'); // pending, paid, overdue, failed
            $table->string('payment_method', 50)->nullable(); // bank_transfer, cash, deduction, etc.

            // 补缴信息
            $table->boolean('is_makeup_payment')->default(false); // 是否补缴
            $table->string('makeup_reason', 200)->nullable(); // 补缴原因
            $table->date('original_due_date')->nullable(); // 原应缴日期

            // 追溯补缴
            $table->boolean('is_retroactive')->default(false); // 是否追溯补缴
            $table->string('retroactive_period', 50)->nullable(); // 追溯期间
            $table->decimal('retroactive_amount', 10, 2)->default(0); // 追溯金额

            // 财务信息
            $table->string('payment_reference', 100)->nullable(); // 缴费凭证号
            $table->string('bank_reference', 100)->nullable(); // 银行流水号
            $table->date('confirmation_date')->nullable(); // 确认日期

            // 逾期信息
            $table->boolean('is_overdue')->default(false);
            $table->integer('overdue_days')->default(0);
            $table->decimal('late_fee', 8, 2)->default(0); // 滞纳金

            // 审批信息
            $table->string('approval_status', 20)->default('approved'); // pending, approved, rejected
            $table->unsignedBigInteger('approved_by')->nullable();
            $table->string('approved_by_name', 100)->nullable();
            $table->datetime('approved_at')->nullable();

            // 备注和附件
            $table->text('notes')->nullable();
            $table->json('attachments')->nullable();

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['insurance_record_id', 'payment_period']);
            $table->index(['employee_id', 'payment_date']);
            $table->index(['payment_status']);
            $table->index(['payment_period']);
            $table->index(['payment_date']);
            $table->index(['due_date']);
            $table->index(['is_overdue']);
            $table->index(['is_makeup_payment']);
            $table->index(['is_retroactive']);
            $table->index(['payment_number']);

            // 外键约束
            $table->foreign('insurance_record_id')->references('id')->on('insurance_records')->onDelete('cascade');
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
            $table->foreign('approved_by')->references('id')->on('users')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('insurance_payments');
    }
};
